/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.businessLayer;

import mnstudent.dataacesslayer.PositionDAL;
import mnstudent.datatranferobject.PositionDTO;
import mnstudent.datatranferobject.StaffDTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.swingx.JXTable;

/**
 *
 * @author 11520_000
 */
public class PositionBLL {
    
    private PositionDAL m_PositionDAL;
    
    public PositionBLL()
    {
        m_PositionDAL = new PositionDAL();
    }
    
    //<editor-fold defaultstate="collapsed" desc="Method">
    
     /**
     * Ham lay tat ca cac Nhan vien
     *
     * @return ResultSet
     */
    public ResultSet getAllPosition(){
        
        return m_PositionDAL.getAllPosition();
    }

    public ArrayList<PositionDTO> getAllPositionToArray()
     {
         ArrayList<PositionDTO> arrPosition= new ArrayList<>();
        try {
            ResultSet rs = getAllPosition();
            while(rs.next())
            {
                PositionDTO _position = new PositionDTO(rs.getNString(1), rs.getNString(2), rs.getBoolean(3),
                       rs.getBoolean(4), rs.getBoolean(5), rs.getBoolean(6), 
                        rs.getBoolean(7), rs.getBoolean(8), rs.getBoolean(9),
                        rs.getBoolean(10), rs.getBoolean(11), rs.getBoolean(12));
                arrPosition.add(_position);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return arrPosition;
     }
     
     
    public ResultSet getAllIdAndNameOfPositionFromDatabase()
    {
         return m_PositionDAL.getAllIdAndNameOfPositionFromDatabase();
    }
    
    public ArrayList<PositionDTO> getAllIdAndNameOfPositionToArray()
     {
        ArrayList<PositionDTO> arrPosition= new ArrayList<>();
        try {
            ResultSet rs = getAllIdAndNameOfPositionFromDatabase();
            while(rs.next())
            {
                System.out.println(rs.getNString(1) + " - " + rs.getNString(2));
                PositionDTO _position = new PositionDTO(rs.getNString(1), rs.getNString(2));
                arrPosition.add(_position);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return arrPosition;
     }
    /**
     * Ham lay khoa theo ma khoa
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getPositionById(String _id)
    {
        return m_PositionDAL.getPositionById(_id);
    }

    public PositionDTO getPositionDTOFullById(String _id)
    {
        PositionDTO positionObj = null;
        ResultSet rs = getPositionById(_id);
        try {
            while(rs.next())
            {
                positionObj = new PositionDTO(rs.getNString(1), 
                        rs.getNString(2), 
                        rs.getBoolean(3),
                        rs.getBoolean(4),
                        rs.getBoolean(5),
                        rs.getBoolean(6),
                        rs.getBoolean(7),
                        rs.getBoolean(8),
                        rs.getBoolean(9),
                        rs.getBoolean(10),
                        rs.getBoolean(11),
                        rs.getBoolean(12)
                );
            }
        } catch (SQLException ex) {
           ex.printStackTrace();
           return null;
        }
        return positionObj;
    }
    
    public PositionDTO getPositionDTOById(String _id)
    {
        PositionDTO obj = null;
        ResultSet rs = getPositionById(_id);
        try {
            while(rs.next())
            {
                obj = new PositionDTO(rs.getNString(1), rs.getNString(2));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return obj;
    }
    public ResultSet getIdAndNameOfPositionById(String _id)
    {
        return m_PositionDAL.getIdAndNameOfPositionById(_id);
    }
     /**
     * ham them moi khoa xuong database
     * @param _position
     * @return true: success, false: fail
     */
    public boolean insertPositionToDatabase(PositionDTO _position)
    {
       return m_PositionDAL.insertPositionToDatabase(_position);
    }
    
    /**
     * Ham cap nhat sua doi thong tin khoa
     * @param _position
     * @return tue: thanh cong, false: that bai
     */
    public boolean updatePositionToDatabase(PositionDTO _position)
    {
        return m_PositionDAL.updatePositionToDatabase(_position);
    }
     
    /**
     * Ham xoa mot khoa ra khoi database
     * @param _position
     * @return 
     */
     public boolean deletePositionFromDatabase(PositionDTO _position)
    {
       
        return m_PositionDAL.deletePositionFromDatabase(_position);
    }
     
    public boolean checkInputIsInvalid(JXTable _jtb, String _input)
     {
        for(int i = 0; i<_jtb.getRowCount(); i++)
        {
            if(_jtb.getSelectedRow() != i){

                if(_input.replaceAll("\\s+", "").toLowerCase().equals(_jtb.getValueAt(i, 1).toString().replaceAll("\\s+", "").toLowerCase())){
                    return false;
                }
            }
        }
          return true;
     }
    //</editor-fold>
}
